/*****************************************************************************************
   
   Copyright (c) Microsoft Corporation. All rights reserved.

   Use of this code sample is subject to the terms of the Microsoft
   Public License, a copy of which should always be distributed with
   this file.  You can also access a copy of this license agreement at:
   http://www.microsoft.com/opensource/licenses.mspx#Ms-PL

 ****************************************************************************************/
using System;

namespace Microsoft.CSA.FeedSync.Utilities
{
    class FeedSyncConvert
    {
        static string s_SourcePath;

        static int s_IDSequenceBase;

        static void Main(string[] i_Arguments)
        {
            try
            {
                //  Validate arguments
                if (i_Arguments.Length < 1)
                {
                    Microsoft.CSA.FeedSync.Utilities.FeedSyncConvert.DisplayUsage(null);
                    return;
                }

                //  Get required parameters
                s_SourcePath = i_Arguments[0];

                //  Get optional parameters
                if (i_Arguments.Length > 1)
                    s_IDSequenceBase = System.Convert.ToInt32(i_Arguments[1]);

                //  Create new instance of xml document
                System.Xml.XmlDocument SourceXmlDocument = new System.Xml.XmlDocument();

                try
                {
                    //  Load the feed contents into xml document
                    SourceXmlDocument.Load(s_SourcePath);
                }
                catch (System.Exception Exception)
                {
                    System.Console.WriteLine("Exception while loading '" + s_SourcePath + "': " + Exception.Message);
                    return;
                }

                System.Xml.XmlNamespaceManager XmlNamespaceManager = new System.Xml.XmlNamespaceManager(SourceXmlDocument.NameTable);

                //  Add feedsync namespace to document
                System.Xml.XmlElement ChannelXmlElement = (System.Xml.XmlElement)SourceXmlDocument.DocumentElement.SelectSingleNode(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RSS_CHANNEL_ELEMENT);
                if (ChannelXmlElement == null)
                    throw new System.Exception("No channel element found!");

                //  Iterate items
                System.Xml.XmlNodeList ItemXmlNodeList = ChannelXmlElement.SelectNodes(Microsoft.CSA.FeedSync.FeedSyncLibrary.Constants.RSS_ITEM_ELEMENT_NAME);

                foreach (System.Xml.XmlElement ItemXmlElement in ItemXmlNodeList)
                {
                    Microsoft.CSA.FeedSync.FeedSyncLibrary.Sync Sync = new Microsoft.CSA.FeedSync.FeedSyncLibrary.Sync
                        (
                        SourceXmlDocument,
                        XmlNamespaceManager,
                        "FeedSyncConvert.exe",
                        (++s_IDSequenceBase).ToString()
                        );

                    if (ItemXmlElement.ChildNodes.Count > 0)
                        ItemXmlElement.InsertBefore(Sync.XmlElement, ItemXmlElement.ChildNodes[0]);
                    else
                        ItemXmlElement.AppendChild(Sync.XmlElement);
                }

                Microsoft.CSA.FeedSync.FeedSyncLibrary.RSSFeed RSSFeed = new Microsoft.CSA.FeedSync.FeedSyncLibrary.RSSFeed(SourceXmlDocument);

                //  Output updated feed
                System.Console.WriteLine(RSSFeed.XmlDocument.OuterXml);
            }
            catch (System.Exception Exception)
            {
                System.Console.WriteLine("Exception encountered: " + Exception.Message);
            }
        }

        static void DisplayUsage(string i_Text)
        {
            string Text = "Usage:\r\nFeedSyncConvert [SourcePath] [IDSequenceBase] \r\n\r\nParameters:\r\n  SourcePath=fully qualified filename of original RSS document\r\n  IDSequenceBase=integral value that will be used as starting id value (optional - default is random guid generation) \r\n";

            //  If text was provided, prepend it to default usage text
            if (!System.String.IsNullOrEmpty(i_Text))
                Text = i_Text + "\r\n\r\n" + Text;

            System.Console.WriteLine(Text);
        }
    }
}
